// 不够灵活，不用了

import { RouteRecordRaw } from 'vue-router'

// 获取布局路由
const layouts = import.meta.glob('../../src/layouts/*.vue')
const layoutsChildRoutes = import.meta.glob('../../src/views/**/*.vue')
console.log(layoutsChildRoutes)
const getRouterByModule = (key: string, values: () => Promise<unknown>, layoutsName?: string) => {
   const names = key.split('/').pop()?.split('.').shift()
   const name = `${layoutsName ? layoutsName + '.' + names : names}`
   const router = {
      name,
      path: `/${layoutsName ? layoutsName + '/' + names : names}`,
      component: values
   } as RouteRecordRaw
   return router
}

const getChileRoutes = (layouts: RouteRecordRaw) => {
   const routes = [] as RouteRecordRaw[]
   Object.entries(layoutsChildRoutes).forEach(([key, values]) => {
      if (key.includes(`../views/${layouts.name as string}`)) {
         routes.push(getRouterByModule(key, values, layouts.name as string))
      }
   })

   return routes
}

const getLayoutRouter = () => {
   const layoutsRouter = [] as RouteRecordRaw[]
   Object.entries(layouts).forEach(async ([key, values]) => {
      const router = getRouterByModule(key, values)
      router.children = getChileRoutes(router)
      layoutsRouter.push(router)
   })

   return layoutsRouter
}

const obj = new Object()

export default getLayoutRouter()
